/*
 * ColophonGenerator.java
 *
 *  created: 21.8.2011
 *  charset: UTF-8
 *  license: MIT (X11) (See LICENSE file for full license)
 */
package cz.mp.k3bg.core;

import cz.mp.k3bg.log.LoggerManager;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Locale;
import java.util.logging.Logger;
import static cz.mp.k3bg.Application.EOL;
import cz.mp.k3bg.util.TagUtils;

/**
 * Třída {@code ColophonGenerator} vytváří jednoduchý HTML dokument se
 * jménem knihy a autora a s krátkým vydavatelským záznamem.
 * 
 * @author Martin Pokorný
 * @version 0.1
 */
public class ColophonGenerator extends Generator {

    private static final boolean DEBUG = false;
    private static final Logger logger =
            LoggerManager.getLogger(ColophonGenerator.class, DEBUG);
    
    /** Krátký text vydavatelského záznamu. */
    private String textOfCopyright;
    
    // -----

    public ColophonGenerator(BookFiles bookFiles, Metadata metadata) {
        super(bookFiles, metadata);
        this.textOfCopyright = createMinimalTextOfCopyright();
    }
    
    // -----
    
    /**
     * 
     * @throws IOException 
     */
    @Override
    public void generate() throws IOException {
        logger.info("");
        if (bookFiles.getColophon() == null) {
            throw new IllegalStateException("colophon=null");
        }
        
        BufferedWriter writer = bookFiles.getColophon().getBufferedWriter();
        
        HtmlHeaderBuilder headerBuilder = new HtmlHeaderBuilder();
        headerBuilder.setCharsetName(bookFiles.getColophon().getCharsetName());
                
        // sestavení souboru :
        writer.write(headerBuilder.createHtmlHeader());
        writer.flush();
        
        writer.write(createBookTitle());
        writer.flush();
        
        if (textOfCopyright != null && ! textOfCopyright.isEmpty()) {
            writer.write("<div class=\""
                    + StyleClassNames.PAGEBREAK_BEFORE.getRealName()
                    + "\" />");
            writer.write(EOL);
            writer.write("<div class=\""
                    + StyleClassNames.COLOPHON.getRealName()
                    + "\">");
            writer.write(EOL);
            writer.write(
                    TagUtils.convertToPrimitiveHtml(textOfCopyright));
            writer.write("</div>");
            writer.write(EOL);
            writer.write(EOL);
        }
        
        writer.write(headerBuilder.createHtmlFooter());
        writer.flush();
        
        writer.flush();
        writer.close();
        
        bookFiles.getColophon().initExisting();
                
        logger.fine("(finished!)");
    }

    /**
     * 
     */
    private String createBookTitle() {
        logger.finer("");
        StringBuilder sb = new StringBuilder();
        sb.append("<a name=\"Begin\" />").append(EOL);
        sb.append("<h1 class=\"")
                .append(StyleClassNames.BOOK_TITLE.getRealName()).append("\">")
                .append(metadata.getAuthor())
                .append("<br><br><br><br><br>")
                .append(metadata.getTitle().toUpperCase(Locale.getDefault()))
                .append("</h1>")
                .append(EOL);
        // pozn. autor <br><br><br><br><br> TITUL -- kvůli převodům (v Calibre), aby se nedělilo na dvě strany
        sb.append(EOL);
        return sb.toString();
    }    
    
    /**
     * (použije se  StringUtils.convertToPrimitiveHtml)
     * 
     * @param textOfCopyright 
     */
    public void setTextOfCopyright(String textOfCopyright) {
        logger.finer("");
        if (textOfCopyright == null) {
            throw new IllegalArgumentException("textOfCopyright=null");
        }
        this.textOfCopyright = textOfCopyright;
    }

    /**
     * 
     * @return 
     */
    public String getTextOfCopyright() {
        return textOfCopyright;
    }
    
   
    /**
     * Z metadat vytvoří velice jednoduchý minimální text tiráže.
     * 
     * @return 
     */
    private String createMinimalTextOfCopyright() {
        logger.finer("");
        if (metadata == null) {
            return "";
        }
        
        StringBuilder sb = new StringBuilder();
        if (metadata.getTitleOriginal() != null 
                && ! metadata.getTitleOriginal().isEmpty()) {
            sb.append(metadata.getTitleOriginal()).append(EOL).append(EOL);
        }
        
        if (metadata.getAuthor() != null 
                && ! metadata.getAuthor().isEmpty()) {
            sb.append("Copyright \u00A9 ");
            sb.append(metadata.getAuthor()).append(EOL).append(EOL);
        }        
        
        if (metadata.getId() != null
                && ! metadata.getId().isEmpty()) {
            if (metadata.isIdIsbn()) {
                sb.append("ISBN ");
            }
            sb.append(metadata.getId());
        }
        return sb.toString();
    }        
    
}   // ColophonGenerator
